package com.lengyu.iottransfer.gateway.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lengyu.iottransfer.gateway.entity.DeviceTransfer;
import com.lengyu.iottransfer.gateway.mapper.DeviceTransferMapper;
import com.lengyu.iottransfer.gateway.service.IDeviceTransferService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liuyu
 * @since 2023-11-02
 */
@Service
public class DeviceTransferServiceImpl extends ServiceImpl<DeviceTransferMapper, DeviceTransfer> implements IDeviceTransferService {

    @Override
    public DeviceTransfer getDeviceTransferByGatewaySendTopic(String gatewaySendTopic) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("gateway_send_topic",gatewaySendTopic);
        DeviceTransfer deviceTransfer = getOne(queryWrapper,false);
        return deviceTransfer;
    }

    @Override
    public DeviceTransfer getDeviceTransferByCode(String code) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("code",code);
        DeviceTransfer deviceTransfer = getOne(queryWrapper,false);
        return deviceTransfer;
    }

    public String getThirdPlatPostPropertyTopic(DeviceTransfer deviceTransfer){
        if(deviceTransfer.getThirdPlatCode().equals("IITA")){
            return  "/sys/" + deviceTransfer.getThirdPlatProductCode() + "/" + deviceTransfer.getThirdPlatDeviceId() + "/s/event/property/post";
        }
        else{
            return null;
        }

    }

    @Override
    public String getIotPlatPropertyTopic(DeviceTransfer deviceTransfer) {
        return  "/lengyu_iot/" + deviceTransfer.getType() + "/" + deviceTransfer.getCode() + "/s/event/property/post";
    }

    @Override
    public String getIotPlatPropertyTopicV1(String deviceType) {
        return  "/lengyu_iot/" + deviceType + "/current/property/post";
    }


    public  String getConnectTopic(String topic){
        return "/sys/client/connected";
    }

    public String getClientIdForIITA(DeviceTransfer deviceTransfer){
        return  deviceTransfer.getThirdPlatProductCode() + "_" + deviceTransfer.getThirdPlatDeviceId() + "_" + "model";
    }
}
